angular.module('sampleApp', ['ngRoute', 'ui.bootstrap', 'appRoutes', 'site', 'main', 'user', 'userProfile', 'ui.bootstrap.demo'])
.config(function($httpProvider) {
    $httpProvider.interceptors.push('loadingStatusInterceptor');
})
.factory('appService', ['$timeout', function($timeout){
    function AppService(){
        this.status = "";
        this.statusMsg = "";
    }

    AppService.prototype.showStatus = function(status, statusMsg, time) {
        var self = this;
        var t = undefined;
        self.status = status;
        self.statusMsg = statusMsg;

        if (t != null && t != undefined) {
            $timeout.cancel(t);
        }

        t = $timeout(function() {
            self.status = "";
            self.statusMsg = "";
        }, time == undefined ? 3000 : time);
    };

    AppService.prototype.setLoadingStatus = function(){
        this.status = "loading";
        this.statusMsg = "Loading...";
    };

    AppService.prototype.hideStatus = function(){
        this.status = "";
        this.statusMsg = "";
    };

    AppService.prototype.isTouchDevice = function(){
        return navigator.userAgent.match(/(Android|webOS|iPhone|iPad|iPod|Touch)/g);
    };

    return new AppService();
}])
.factory('loadingStatusInterceptor', [ '$q', '$rootScope', 'appService' ,function($q, $rootScope, appService) {
    var activeRequests = 0;
    var started = function() {
        if(activeRequests==0) {
            appService.setLoadingStatus();
        }
        activeRequests++;
    };
    var ended = function() {
        activeRequests--;
        if(activeRequests==0) {
            appService.hideStatus();
        }
    };
    return {
        request: function(config) {
            var extension = config.url.split(".")[1];
            if (!extension  || (extension && extension === 'json')){
                started();
            }
            return config || $q.when(config);
        },
        response: function(response) {
            var extension = response.config.url.split(".")[1];
            if (!extension  || (extension && extension === 'json')){
                activeRequests--;
                if (response.config.method === "POST"){
                    if (response.data.status && response.data.status !== "SUCCESS") {
                        if(activeRequests==0) {
                        	console.log(response);
                            if (response.data.msg) {
                                appService.showStatus("error", response.data.msg);
                        	} else if(response.data.returnObject.type) {
                        		appService.showStatus("error", response.data.returnObject.type);
                        	} else {
                                appService.showStatus("error", "Oops, there was an error!");
                            }
                        };
                        return $q.reject(response);
                    } else {
                        if(activeRequests==0) {
                            appService.showStatus("success", "Success");
                        };
                    }
                } else {
                    appService.hideStatus();
                }
            }

            return response || $q.when(response);
        },
        responseError: function(rejection) {
            var extension = rejection.config.url.split(".")[1];
            if (!extension  || (extension && extension === 'json')){
                ended();
            }
            return $q.reject(rejection);
        }
    };
}]);